import hyRequest from "../..";
//token增删查
let currentToken = "";
export const setCurrentToken = (token: string) => {
  currentToken = token;
  localStorage.setItem("setToken", token);
};
export function getCurrentToken() {
  return localStorage.getItem("setToken") || currentToken;
}
export function clearCurrentToken() {
  currentToken = "";
  localStorage.removeItem("setToken");
}
//获取验证码
export function getLoginCode() {
  return hyRequest.get({
    url: "captchaImage",
  });
}
//登入获取token接口
export function Login(params: {
  username: string;
  password: string;
  code: string;
  uuid: string;
}) {
  return hyRequest
    .post({
      url: "/login",
      data: params,
      headers: {
        "Content-Type": "application/json",
      },
    })
    .then((res) => {
      // console.log(res);
      if (res.token) {
        setCurrentToken(res.token);
      }
      return res;
    });
}

//获取用户接口
export function getUserInfo() {
  const token = getCurrentToken();
  return hyRequest
    .get({
      url: "/getInfo", // 若依用户信息接口
      headers: {
        Authorization: `Bearer ${token}`, // 若依要求的Token格式
      },
    })
    .then((res) => {
      // console.log(res);
      return res;
    });
}
export function Logout() {
  return hyRequest
    .post({
      url: "/logout",
    })
    .finally(() => {
      clearCurrentToken(); // 无论请求成功失败都清理 token
    });
}

